//
//  TEDatabaseObjectViewController.m
//  TintExample
//
//  Created by Kevin Wu on 5/31/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TEDatabaseObjectViewController.h"
#import "TEDBUser.h"
#import "TEDBBuddy.h"


@implementation TEDatabaseObjectViewController



#pragma mark - Memory

- (id)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}


#pragma mark - UIViewController

- (void)loadView {
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addButtonWithTitle:@"Create database" selector:@selector(createDatabase:) type:1];
    [self addButtonWithTitle:@"Create table" selector:@selector(createTable:) type:1];
    [self addButtonWithTitle:@"Query" selector:@selector(query:) type:1];
    
    [self addButtonWithTitle:@"Insert empty" selector:@selector(insertEmpty:) type:2];
    [self addButtonWithTitle:@"Insert part" selector:@selector(insertPart:) type:2];
    [self addButtonWithTitle:@"Insert" selector:@selector(insert:) type:2];
    
    [self addButtonWithTitle:@"Update part" selector:@selector(updatePart:) type:3];
    [self addButtonWithTitle:@"Update" selector:@selector(update:) type:3];
    
    [self addButtonWithTitle:@"Destroy" selector:@selector(destroy:) type:4];
    [self addButtonWithTitle:@"Is exist" selector:@selector(isExist:) type:4];
    [self addButtonWithTitle:@"Reload" selector:@selector(reload:) type:4];
    
    [self addButtonWithTitle:@"Delete all" selector:@selector(deleteObjects:) type:5];
    [self addButtonWithTitle:@"Delete part" selector:@selector(deletePartObjects:) type:5];
    [self addButtonWithTitle:@"Update all" selector:@selector(updateObjects:) type:5];
    [self addButtonWithTitle:@"Update part" selector:@selector(updatePartObjects:) type:5];
    [self addButtonWithTitle:@"Select all" selector:@selector(selectObjects:) type:5];
    [self addButtonWithTitle:@"Select part" selector:@selector(selectPartObjects:) type:5];
}

- (void)createDatabase:(id)sender {
    NSString *path = [TTDocumentsPath() stringByAppendingPathComponent:@"db2.sqlite"];
    TTDatabase *db = [TTDatabase sharedObject];
    db.databasePath = path;
    [db open];
}

- (void)createTable:(id)sender {
    
    [TEDBUser createTable];
    [TEDBBuddy createTable];
    
}


- (void)query:(id)sender {
    NSArray *result = nil;
    
    result = [TEDBUser selectObjectsOrderBy:@"a DESC, c DESC" where:@"a<? AND a>?", [NSNumber numberWithInt:10], [NSNumber numberWithInt:1], nil];
    for (int i=0; i<[result count]; ++i) {
        TEDBUser *user = [result objectAtIndex:i];
        NSLog(@"%@", user);
    }
    
}




- (void)insertEmpty:(id)sender {
    
    TEDBUser *user = nil;
    
    user = [[TEDBUser alloc] init];
    [user save];
    TTRelease(user);
    
    user = [[TEDBUser alloc] init];
    [user save];
    TTRelease(user);
    
    
    
    TEDBBuddy *buddy = nil;
    
    buddy = [[TEDBBuddy alloc] init];
    [buddy save];
    TTRelease(buddy);
    
    buddy = [[TEDBBuddy alloc] init];
    [buddy save];
    TTRelease(buddy);
    
}

- (void)insertPart:(id)sender {
    
    TEDBUser *user = nil;
    
    user = [[TEDBUser alloc] init];
    user.pk = 10001;
    user.a = 100;
    user.b = 1;
    user.c = 115292150460600009L;
    user.d = 1.01234567890123456789+100;
    user.e = [[NSDate date] dateByAddingDays:1000];
    user.f = [NSString randStringWithLength:8];
    [user save:@"a"];
    TTRelease(user);
    
    user = [[TEDBUser alloc] init];
    user.pk = 10001;
    user.a = 100;
    user.b = 1;
    user.c = 115292150460600009L;
    user.d = 1.01234567890123456789+100;
    user.e = [[NSDate date] dateByAddingDays:1000];
    user.f = [NSString randStringWithLength:8];
    [user save:@"d"];
    TTRelease(user);
    
}

- (void)insert:(id)sender {
    
    NSDate *date = [[NSDate date] dateBySubtractingDays:10];
    
    for (int i=0; i<20; ++i) {
        
        TEDBUser *user = [[TEDBUser alloc] init];
        user.a = i+i%2;
        user.b = i%2;
        user.c = 120-i;
        user.d = 1.01234567890123456789+i;
        user.e = [date dateByAddingDays:i];
        user.f = [NSString randStringWithLength:8];
        [user save];
        
        TTRelease(user);
    }
    
}



- (void)updatePart:(id)sender {
    
    TEDBUser *user = [[TEDBUser alloc] init];
    user.pk = 10017;
    user.a = 1010;
    user.b = 1;
    user.c = 115292150460600009L;
    user.d = 1.01234567890123456789+100;
    user.e = [[NSDate date] dateByAddingDays:1000];
    user.f = @"HEHE";
    [user save:@" a, c , f "];
    
    TTRelease(user);
    
}

- (void)update:(id)sender {
    
    TEDBUser *user = [[TEDBUser alloc] init];
    user.pk = 10017;
    user.a = 1010;
    user.b = 1;
    user.c = 115292150460600019L;
    user.d = 1.01234567890123456789+100;
    user.e = [[NSDate date] dateByAddingDays:1000];
    user.f = @"HAHA";
    [user save];
    
    TTRelease(user);
    
}



- (void)destroy:(id)sender {
    TEDBUser *user = [[TEDBUser alloc] init];
    user.pk = 1;
    [user destroy];
    TTRelease(user);
}

- (void)isExist:(id)sender {
    TEDBUser *user = [[TEDBUser alloc] init];
    user.pk = 1;
    if ([user isExist]) {
        NSLog(@"Is exist");
    } else {
        NSLog(@"Is not exist");
    }
    
}

- (void)reload:(id)sender {
    TEDBUser *user = [[TEDBUser alloc] init];
    user.pk = 10009;
    [user reload];
    NSLog(@"%@", user);
    TTRelease(user);
}

- (void)deleteObjects:(id)sender {
    [TEDBUser deleteAllObjects];
}
- (void)deletePartObjects:(id)sender {
    [TEDBUser deleteObjectsWhere:@"a<? AND a>?", 
     [NSNumber numberWithInt:10], 
     [NSNumber numberWithInt:1], nil];
}
- (void)updateObjects:(id)sender {
    [TEDBUser updateAllObjectsSet:@"f='HE'"];
}
- (void)updatePartObjects:(id)sender {
    [TEDBUser updateObjectsSet:@"f='HEH'" 
                         where:@"a<? AND a>?", 
     [NSNumber numberWithInt:10], 
     [NSNumber numberWithInt:1], nil];
}
- (void)selectObjects:(id)sender {
    [TEDBUser selectAllObjects];
}
- (void)selectPartObjects:(id)sender {
    NSArray *result = nil;
    result = [TEDBUser selectObjectsOrderBy:@"a DESC, c DESC" where:@"a<? AND a>?", [NSNumber numberWithInt:10], [NSNumber numberWithInt:1], nil];
    for (int i=0; i<[result count]; ++i) {
        TEDBUser *user = [result objectAtIndex:i];
        NSLog(@"%@", user);
    }
}

@end
